package app.excel.cartoon.word;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import app.esb.common.DBExcutor;
import cn.lily.com.container.data.Record;
import cn.lily.com.db.container.reader.DataTable;

/**
 * 导出数据统计类
 * @author JiaJ
 *
 */
public class ExcelWordData {

	/**
	 * word - cartoonID
	 */
	private Map<String,List<String>> wordMap = new HashMap<String, List<String>>();
	
	/**
	 * cartoonID - cartoonDetailID
	 */
	private Map<String,List<Record>> cartoonMap = new HashMap<String,List<Record>>();
	
	/**
	 * cartoonID - Int
	 */
	private Map<String,Integer> cartoonSizeMap = new HashMap<String, Integer>();
	
	private DataTable dataTable;
	
	/**
	 * 装载数据
	 */
	public void instData(){
		
		String sql = "select detail.*,"
						  + "cartoon.CAN_NAME,cartoon.CAN_NAME_OTHER,cartoon.CAN_NAM_ENG,"
						  + "cartoon.CAN_AREA,cartoon.CAN_SOURCE,cartoon.CAN_FIRST,cartoon.CAN_DEMO,"
						  + "res.RES_STORE_PLACE,res.RES_STORE_SIZE,res.DISK_ID,res.RES_TAG_MEMO "
				   + "from LEO_CAN_DETAIL_INFO detail "
				   + "inner join LEO_CAN_INFO cartoon on detail.CAN_ID=cartoon.CAN_ID "
				   + "inner join LEO_RES_INFO res on detail.RES_ID=res.RES_ID "
				   + "order by detail.CAN_TIME ASC,detail.CAN_KIND DESC ";
		
		this.dataTable = DBExcutor.getDBManager().executeQueryTable(sql);
		
	}
	
	/**
	 * 分析数据
	 */
	public void excuteData(){
		
		while(this.dataTable.next()){
			
			String canID = this.dataTable.getString("CAN_ID");
			String firstWord = this.dataTable.getString("CAN_FIRST");
			
			//数据准备
			this.readyData(canID, firstWord);
			
			//集合放入
			List<Record> recordList = this.cartoonMap.get(canID);
			recordList.add(this.dataTable.getRecord());
			
			//体积
			int old = this.cartoonSizeMap.get(canID);
			int nw = old + this.dataTable.getInt("CAN_TOTAL");
			this.cartoonSizeMap.put(canID, nw);
			
		}
		
		System.out.println("数据分析完成..............");
		
	}
	
	/**
	 * 数据准备
	 * @param canID
	 * @param firstWord
	 */
	private void readyData(String canID,String firstWord){
		
		if(!wordMap.containsKey(firstWord)){
			List<String> wordList = new ArrayList<String>();
			this.wordMap.put(firstWord, wordList);
		}
		
		if(!this.cartoonMap.containsKey(canID)){
			List<Record> recordList = new ArrayList<Record>();
			this.cartoonMap.put(canID, recordList);
			this.cartoonSizeMap.put(canID, 0);
		}
		
		List<String> cartoonList = this.wordMap.get(firstWord);
		if(!cartoonList.contains(canID)){
			cartoonList.add(canID);
		}
	}
	
	/**
	 * 获取动漫标示集合
	 * @param word
	 * @return
	 */
	public List<String> getCartoonList(String word){
		return this.wordMap.get(word);
	}

	public Map<String, List<String>> getWordMap() {
		return wordMap;
	}
	
	/**
	 * 获取动漫详细信息
	 * @param cartoonID
	 * @return
	 */
	public List<Record> getCartoonDetail(String cartoonID){
		return this.cartoonMap.get(cartoonID);
	}

	public Map<String, List<Record>> getCartoonMap() {
		return cartoonMap;
	}

	/**
	 * 获取动漫集数
	 * @param cartoonID
	 * @return
	 */
	public int getCartoonSize(String cartoonID){
		return this.cartoonSizeMap.get(cartoonID);
	}
	
	public Map<String, Integer> getCartoonSizeMap() {
		return cartoonSizeMap;
	}
	
}
